package com.liuxn.dubbo.sample.demo.filter;

import cn.xs.ambi.bas.http.IpUtils;
import com.liuxn.dubbo.sample.demo.util.wrapper.RequestWrapper;
import com.yomahub.tlog.constant.TLogConstants;
import com.yomahub.tlog.context.TLogContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author liuxn
 * @date 2024/10/10
 */
@Slf4j
public class TokenFilter extends OncePerRequestFilter {

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws ServletException, IOException {
        RequestWrapper wrapper = new RequestWrapper(request);
        //wrapper.addHeader("header.text", "test");
        String ipAddr = IpUtils.getIpAddr(request);
        wrapper.addHeader(TLogConstants.PRE_IP_KEY, ipAddr);
        wrapper.addHeader(TLogConstants.TLOG_TRACE_KEY, TLogContext.getTraceId());
        log.info(">> 设置header，test");
        chain.doFilter(wrapper,response);
    }
}
